// pages/childattendanceone/childattendanceone.js
const util = require('../../utils/util.js')
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true, //隐藏表单控件
    page: 1, //当前请求数据是第几页
    pageSize: 10, //每页数据条数
    hasMoreData: true, //上拉时是否继续请求数据，即是否还有更多数据
    contentlist: [],
    attendanceList: [],
    date: util.formatDate(new Date()),
    ru:'../../images/icon_ru.png',
    li:'../../images/icon_li.png',
    card:'../../images/icon_card.png',
    shou:'../../images/icon_shouqian.png',
    
  },
  btnqingjia:function (e) {
    wx.navigateTo({
      url: '../childattendanceday/childattendanceday'
    })
  },
  btnyuedu:function (e) {
    wx.navigateTo({
      url: '../childattendanceclassstudent/childattendanceclassstudent'
    })
  },
  btnmeiri:function (e) {
    wx.navigateTo({
      url: '../childattendanceday/childattendanceday'
    })
  },
  btndetailclick: function (e) {
    var contentliststr = JSON.stringify(this.data.contentlist)
    wx.navigateTo({
      url: '../childattendanceclass/childattendanceclass?contentliststr=' + contentliststr
    })
  },
  previewImg:function(e){
    var index=e.target.dataset.index;
    var current=e.target.dataset.src;
    var that=this;
    var imgArr = [];

    var url1=that.data.attendanceList[index].PunchPhoto
    if(url1!=null && url1.length>0){
      imgArr.push(url1);
    }


    var url=that.data.attendanceList[index].PunchPic;
    if(url!=null && url.length>0){
      imgArr.push(url);
    }
  
    wx.previewImage({
      current: current,     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 获取分页列表
  getInfo: function (message) {
    var that = this;
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: message,
    })
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "date": this.data.date,
      "pagenum": that.data.page,
      "pagesize": that.data.pageSize,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_kidattendanceall"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            contentlist:res.data.attendanceclasslist
          })
          var attendanceListTem =  that.data.attendanceList;
 
          if (res.data.attendanceList.length > 0) {
            wx.hideNavigationBarLoading() //在当前页面隐藏导航条加载动画
            wx.hideLoading() //隐藏 loading 提示框
            if (that.data.page == 1) {
              attendanceListTem = []
            }
            var contentlist = res.data.attendanceList;
            if (contentlist.length < that.data.pageSize) {
              that.setData({
                attendanceList: attendanceListTem.concat(contentlist),
                hasMoreData: false
              })
            } else {
              that.setData({
                attendanceList: attendanceListTem.concat(contentlist),
                hasMoreData: true,
                page: that.data.page + 1
              })
            }
          } else {
            wx.hideNavigationBarLoading()
            wx.hideLoading()
          }
        } else {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
          wx.showToast({
            title: "获取数据失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInfo("正在加载数据...")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.page = 1
    this.getInfo('正在刷新数据')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.getInfo('加载更多数据')
    } else {
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})